package com.jcl.recruit.resume.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jcl.core.common.page.PageResult;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.core.constants.ResumeLogType;
import com.jcl.recruit.core.web.utils.RequestUtils;
import com.jcl.recruit.log.logic.ResumeLogLogic;
import com.jcl.recruit.resume.logic.ResumeCommonLogic;
import com.jcl.recruit.resume.vo.ResumeResultEntity;
import com.jcl.recruit.resume.vo.ResumeSimpleResultEntity;
import com.jcl.recruit.resume.vo.ResumeVo;
import com.jcl.recruit.user.logic.UserResumeAttentionLogic;
import com.jcl.recruit.user.model.UserResumeAttention;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘明新
 * @date 2018/8/3 下午2:49
 */
@Service
public class ResumeService {

    @Autowired
    private ResumeCommonLogic resumeCommonLogic;

    @Autowired
    private ResumeLogLogic resumeLogLogic;

    @Autowired
    private UserResumeAttentionLogic userResumeAttentionLogic;

    public PageResult pageListByUserId(ResumeVo resumeVo) {

        String corpCode = RequestUtils.getCorpCode();
        String userId = RequestUtils.getUserId();

        List<Integer> resumeIds = null;

        if (null != resumeVo.getCategory() && ResumeLogType.TYPE_ADD_ATTENTION == resumeVo.getCategory()) {
            List<UserResumeAttention> attentionList = userResumeAttentionLogic.getListByUserId(Integer.valueOf(userId));
            if (CollectionUtils.isNotEmpty(attentionList)) {
                resumeIds = new ArrayList<>();
                for (UserResumeAttention attention : attentionList) {
                    resumeIds.add(attention.getResumeId());
                }
            }
        }
        List<ResumeSimpleResultEntity> resumeList = null;
        PageHelper.startPage(resumeVo.getCurPage(), resumeVo.getPageSize());
        if (null != resumeVo.getCategory() && ResumeLogType.TYPE_ADD_ATTENTION == resumeVo.getCategory()) {
            if (CollectionUtils.isNotEmpty(resumeIds)) {
                resumeList = resumeCommonLogic.findSimpleList(corpCode, resumeIds);
                for (ResumeSimpleResultEntity entity : resumeList) {
                    entity.setAttention(true);
                }
            } else {
                resumeList = new ArrayList<>();
            }
        } else {

            Integer _userId = Integer.valueOf(userId);
            List<Integer> _resumeIds = resumeLogLogic.findResumeIds(corpCode, _userId, resumeVo.getCategory());
            if (CollectionUtils.isNotEmpty(_resumeIds)) {
                resumeList = resumeCommonLogic.findSimpleList(corpCode, _resumeIds);
            }

            userResumeAttentionLogic.changeResumeAttentionList(_userId, resumeList);
        }

        PageInfo info = new PageInfo(resumeList);
        PageResult result = new PageResult(resumeVo.getCurPage(), resumeVo.getPageSize(), info.getTotal(), resumeList);

        return result;
    }

    public ResumeResultEntity findResumeDetail(Integer resumeId){

        String corpCode = RequestUtils.getCorpCode();
        return resumeCommonLogic.findDetailEntity(corpCode, resumeId);
    }

}
